﻿using System;
using System.Threading.Tasks;

using Tszy.Unlimited.Data.AdoNet.Sql.Providers;

namespace Tszy.Unlimited.Data.AdoNet.Sql.Components.Db.Mysql
{
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="TSource"></typeparam>
    public class ExecuteCleaner<TSource> : IExecuteCleaner<TSource> where TSource : new()
    {
        #region 字段

        private readonly IDataProvider _dataProvider;

        /// <summary>
        /// 是否显示 SQL。
        /// </summary>
        private bool _showSql = false;

        /// <summary>
        /// 输出 SQL 方法。
        /// </summary>
        private Action<string> _printSql = null;

        #endregion 字段

        #region 构造器

        internal ExecuteCleaner(ICleaner<TSource> cleaner, IDataProvider dataProvider, bool showSql = false, Action<string> printSql = null)
        {
            Cleaner = cleaner ?? throw new ArgumentNullException(nameof(cleaner));

            _dataProvider = dataProvider;

            _showSql = showSql;
            _printSql = printSql;
        }

        #endregion 构造器

        #region 属性

        /// <summary>
        ///
        /// </summary>
        public ICleaner<TSource> Cleaner { get; set; }

        #endregion 属性

        #region 执行

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool Execute()
        {
            //var where = Cleaner.Context.Where ?? string.Empty;

            //var updateBuilder = new StringBuilder();

            //updateBuilder.Append($"DELETE FROM {GetTableName()}");

            //if (where.Length > 0)
            //{
            //    updateBuilder.Append($" WHERE {where}");
            //}

            //var sql = updateBuilder.ToString();

            //DbModelDebug.Debug(sql, Cleaner.Context.ParameterValues);

            //return Cleaner._dataManager.Execute(sql, Cleaner.Context.ParameterValues, Cleaner.Context.DbTransaction);
            var sql = SqlBuilder.BuildDelete("", Cleaner.Context, _showSql, _printSql);

            return _dataProvider.Execute(sql, Cleaner.Context.ParameterValues) > 0;
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ExecuteAsync()
        {
            //var where = Cleaner.Context.Where ?? string.Empty;

            //var updateBuilder = new StringBuilder();

            //updateBuilder.Append($"DELETE FROM {GetTableName()}");

            //if (where.Length > 0)
            //{
            //    updateBuilder.Append($" WHERE {where}");
            //}

            //var sql = updateBuilder.ToString();

            //DbModelDebug.Debug(sql, Cleaner.Context.ParameterValues);

            //return await Cleaner._dataManager.ExecuteAsync(sql, Cleaner.Context.ParameterValues, Cleaner.Context.DbTransaction);
            var sql = SqlBuilder.BuildDelete("", Cleaner.Context, _showSql, _printSql);

            return await _dataProvider.ExecuteAsync(sql, Cleaner.Context.ParameterValues) > 0;
        }

        #endregion 执行
    }
}